import cache from '../../utils/cache';
import { v4 as uuidv4 } from 'uuid';

const state = {
	token:  cache.get('token') || '',
	userInfo: cache.get('userInfo'),
	deviceToken: cache.get('deviceToken') || '',
};

const mutations = {
	LOGIN(state, opt) {
		state.token = opt;
		cache.set('token', opt, opt.expire);
	},
	SET_DEVICE_TOKEN(state, token) {
		state.deviceToken = token;
		cache.set('deviceToken', token);
	},
}

const actions = {
	generateDeviceToken({ commit }) {
		// 检查是否已经存在设备token
		const existingToken = cache.get('deviceToken');
		if (!existingToken) {
			const token = uuidv4();
			// 设置token，过期时间为1天（24小时）
			cache.set('deviceToken', token, 24 * 60 * 60);
			commit('SET_DEVICE_TOKEN', token);
		}
	},
};

export default {
	state,
	mutations,
	actions
};
